home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2005 October / PCWOCT05.iso / Software / FromTheMag / XAMPP 1.4.14 / xampp-win32-1.4.14-installer.exe / xampp / php / pear / OS / Guess.php
PHP Script  |  2004-10-01  |  8KB  |  266 lines

  1. <?php
  2. //
  3. // +----------------------------------------------------------------------+
  4. // | PHP Version 5                                                        |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2004 The PHP Group                                |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 3.0 of the PHP license,       |
  9. // | that is bundled with this package in the file LICENSE, and is        |
  10. // | available through the world-wide-web at the following url:           |
  11. // | http://www.php.net/license/3_0.txt.                                  |
  12. // | If you did not receive a copy of the PHP license and are unable to   |
  13. // | obtain it through the world-wide-web, please send a note to          |
  14. // | license@php.net so we can mail you a copy immediately.               |
  15. // +----------------------------------------------------------------------+
  16. // | Authors: Stig Bakken <ssb@php.net>                                   |
  17. // |                                                                      |
  18. // +----------------------------------------------------------------------+
  19. //
  20. // $Id: Guess.php,v 1.13 2004/01/08 17:33:11 sniper Exp $
  21.  
  22. // {{{ uname examples
  23.  
  24. // php_uname() without args returns the same as 'uname -a', or a PHP-custom
  25. // string for Windows.
  26. // PHP versions prior to 4.3 return the uname of the host where PHP was built,
  27. // as of 4.3 it returns the uname of the host running the PHP code.
  28. //
  29. // PC RedHat Linux 7.1:
  30. // Linux host.example.com 2.4.2-2 #1 Sun Apr 8 20:41:30 EDT 2001 i686 unknown
  31. //
  32. // PC Debian Potato:
  33. // Linux host 2.4.17 #2 SMP Tue Feb 12 15:10:04 CET 2002 i686 unknown
  34. //
  35. // PC FreeBSD 3.3:
  36. // FreeBSD host.example.com 3.3-STABLE FreeBSD 3.3-STABLE #0: Mon Feb 21 00:42:31 CET 2000     root@example.com:/usr/src/sys/compile/CONFIG  i386
  37. //
  38. // PC FreeBSD 4.3:
  39. // FreeBSD host.example.com 4.3-RELEASE FreeBSD 4.3-RELEASE #1: Mon Jun 25 11:19:43 EDT 2001     root@example.com:/usr/src/sys/compile/CONFIG  i386
  40. //
  41. // PC FreeBSD 4.5:
  42. // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb  6 23:59:23 CET 2002     root@example.com:/usr/src/sys/compile/CONFIG  i386
  43. //
  44. // PC FreeBSD 4.5 w/uname from GNU shellutils:
  45. // FreeBSD host.example.com 4.5-STABLE FreeBSD 4.5-STABLE #0: Wed Feb  i386 unknown
  46. //
  47. // HP 9000/712 HP-UX 10:
  48. // HP-UX iq B.10.10 A 9000/712 2008429113 two-user license
  49. //
  50. // HP 9000/712 HP-UX 10 w/uname from GNU shellutils:
  51. // HP-UX host B.10.10 A 9000/712 unknown
  52. //
  53. // IBM RS6000/550 AIX 4.3:
  54. // AIX host 3 4 000003531C00
  55. //
  56. // AIX 4.3 w/uname from GNU shellutils:
  57. // AIX host 3 4 000003531C00 unknown
  58. //
  59. // SGI Onyx IRIX 6.5 w/uname from GNU shellutils:
  60. // IRIX64 host 6.5 01091820 IP19 mips
  61. //
  62. // SGI Onyx IRIX 6.5:
  63. // IRIX64 host 6.5 01091820 IP19
  64. //
  65. // SparcStation 20 Solaris 8 w/uname from GNU shellutils:
  66. // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc
  67. //
  68. // SparcStation 20 Solaris 8:
  69. // SunOS host.example.com 5.8 Generic_108528-12 sun4m sparc SUNW,SPARCstation-20
  70. //
  71.  
  72. // }}}
  73.  
  74. /* TODO:
  75.  * - define endianness, to allow matchSignature("bigend") etc.
  76.  */
  77.  
  78. class OS_Guess
  79. {
  80.     var $sysname;
  81.     var $nodename;
  82.     var $cpu;
  83.     var $release;
  84.     var $extra;
  85.  
  86.     function OS_Guess($uname = null)
  87.     {
  88.         list($this->sysname,
  89.              $this->release,
  90.              $this->cpu,
  91.              $this->extra,
  92.              $this->nodename) = $this->parseSignature($uname);
  93.     }
  94.  
  95.     function parseSignature($uname = null)
  96.     {
  97.         static $sysmap = array(
  98.             'HP-UX' => 'hpux',
  99.             'IRIX64' => 'irix',
  100.             // Darwin?
  101.         );
  102.         static $cpumap = array(
  103.             'i586' => 'i386',
  104.             'i686' => 'i386',
  105.         );
  106.         if ($uname === null) {
  107.             $uname = php_uname();
  108.         }
  109.         $parts = split('[[:space:]]+', trim($uname));
  110.         $n = count($parts);
  111.  
  112.         $release = $machine = $cpu = '';
  113.         $sysname = $parts[0];
  114.         $nodename = $parts[1];
  115.         $cpu = $parts[$n-1];
  116.         $extra = '';
  117.         if ($cpu == 'unknown') {
  118.             $cpu = $parts[$n-2];
  119.         }
  120.  
  121.         switch ($sysname) {
  122.             case 'AIX':
  123.                 $release = "$parts[3].$parts[2]";
  124.                 break;
  125.             case 'Windows':
  126.                 switch ($parts[1]) {
  127.                     case '95/98':
  128.                         $release = '9x';
  129.                         break;
  130.                     default:
  131.                         $release = $parts[1];
  132.                         break;
  133.                 }
  134.                 $cpu = 'i386';
  135.                 break;
  136.             case 'Linux':
  137.                 $extra = $this->_detectGlibcVersion();
  138.                 // use only the first two digits from the kernel version
  139.                 $release = ereg_replace('^([[:digit:]]+\.[[:digit:]]+).*', '\1', $parts[2]);
  140.                 break;
  141.             default:
  142.                 $release = ereg_replace('-.*', '', $parts[2]);
  143.                 break;
  144.         }
  145.  
  146.  
  147.         if (isset($sysmap[$sysname])) {
  148.             $sysname = $sysmap[$sysname];
  149.         } else {
  150.             $sysname = strtolower($sysname);
  151.         }
  152.         if (isset($cpumap[$cpu])) {
  153.             $cpu = $cpumap[$cpu];
  154.         }
  155.         return array($sysname, $release, $cpu, $extra, $nodename);
  156.     }
  157.  
  158.     function _detectGlibcVersion()
  159.     {
  160.         // Use glibc's <features.h> header file to
  161.         // get major and minor version number:
  162.         include_once "System.php";
  163.         $tmpfile = System::mktemp("glibctest");
  164.         $fp = fopen($tmpfile, "w");
  165.         fwrite($fp, "#include <features.h>\n__GLIBC__ __GLIBC_MINOR__\n");
  166.         fclose($fp);
  167.         $cpp = popen("/usr/bin/cpp $tmpfile", "r");
  168.         $major = $minor = 0;
  169.         while ($line = fgets($cpp, 1024)) {
  170.             if ($line{0} == '#' || trim($line) == '') {
  171.                 continue;
  172.             }
  173.             if (list($major, $minor) = explode(' ', trim($line))) {
  174.                 break;
  175.             }
  176.         }
  177.         pclose($cpp);
  178.         unlink($tmpfile);
  179.         if (!($major && $minor) && is_link('/lib/libc.so.6')) {
  180.             // Let's try reading the libc.so.6 symlink
  181.             if (ereg('^libc-([.*])\.so$', basename(readlink('/lib/libc.so.6')), $matches)) {
  182.                 list($major, $minor) = explode('.', $matches);
  183.             }
  184.         }
  185.         if (!($major && $minor)) {
  186.             return '';
  187.         }
  188.         return "glibc{$major}.{$minor}";
  189.     }
  190.  
  191.     function getSignature()
  192.     {
  193.         if (empty($this->extra)) {
  194.             return "{$this->sysname}-{$this->release}-{$this->cpu}";
  195.         }
  196.         return "{$this->sysname}-{$this->release}-{$this->cpu}-{$this->extra}";
  197.     }
  198.  
  199.     function getSysname()
  200.     {
  201.         return $this->sysname;
  202.     }
  203.  
  204.     function getNodename()
  205.     {
  206.         return $this->nodename;
  207.     }
  208.  
  209.     function getCpu()
  210.     {
  211.         return $this->cpu;
  212.     }
  213.  
  214.     function getRelease()
  215.     {
  216.         return $this->release;
  217.     }
  218.  
  219.     function getExtra()
  220.     {
  221.         return $this->extra;
  222.     }
  223.  
  224.     function matchSignature($match)
  225.     {
  226.         if (is_array($match)) {
  227.             $fragments = $match;
  228.         } else {
  229.             $fragments = explode('-', $match);
  230.         }
  231.         $n = count($fragments);
  232.         $matches = 0;
  233.         if ($n > 0) {
  234.             $matches += $this->_matchFragment($fragments[0], $this->sysname);
  235.         }
  236.         if ($n > 1) {
  237.             $matches += $this->_matchFragment($fragments[1], $this->release);
  238.         }
  239.         if ($n > 2) {
  240.             $matches += $this->_matchFragment($fragments[2], $this->cpu);
  241.         }
  242.         if ($n > 3) {
  243.             $matches += $this->_matchFragment($fragments[3], $this->extra);
  244.         }
  245.         return ($matches == $n);
  246.     }
  247.  
  248.     function _matchFragment($fragment, $value)
  249.     {
  250.         if (strcspn($fragment, '*?') < strlen($fragment)) {
  251.             $reg = '^' . str_replace(array('*', '?', '/'), array('.*', '.', '\\/'), $fragment) . '$';
  252.             return eregi($reg, $value);
  253.         }
  254.         return ($fragment == '*' || !strcasecmp($fragment, $value));
  255.     }
  256.  
  257. }
  258.  
  259. /*
  260.  * Local Variables:
  261.  * indent-tabs-mode: nil
  262.  * c-basic-offset: 4
  263.  * End:
  264.  */
  265. ?>
  266.